# -*- encoding: utf-8

from memory import *

class Rom (object):
    
    """ Classe contenant les listes d'instructions et de labels. """

    def __init__ (self, mem):
        self.__mem = mem
        self.__listInstruction = []
        self.__dictLabel = {}
        
    def getMemory (self):
        return self.__mem
    
    def getInstruction(self, num):
        return self.__listInstruction[num]
      
    def getLabel(self, nom):
        return self.__dictLabel[nom]
        
    def addInstruction(self, instruction, *args):
        ins = instruction(*args, mem = self.__mem)
        self.__listInstruction.append(ins)
    
    def addLabel(self, nom):
        self.__dictLabel[nom] = len(self.__listInstruction)
    
    def show(self):
        for i in range (0, len(self.__listInstruction)):
            print(self.__listInstruction[i])

    def restart (self):
        self.__mem.setRegister(EPC, 0)
        
    def next (self):
        ins = self.__listInstruction[self.__mem.getRegister(EPC)]
        res = ins.do()   
        if res:
            if type(res) == int:
                self.__mem.setRegister(EPC, res)
            else:
                self.__mem.setRegister(EPC, self.__dictLabel[res])
        else:
            self.__mem.setRegister(EPC, self.__mem.getRegister(EPC) + 1)
        return ins

    def end (self):
        return len(self.__listInstruction) <= self.__mem.getRegister(EPC)
